# -*- coding: latin-1 -*-

###   App By : Redouane            ###
###   E-MAIL : unrealdz@gmail.com  ###
###   BLOG : dzpp.blogspot.com     ### 
###   LICENSE : GNU GPL v3         ###


import re
import urllib
from os import mkdir, path

def striphostname(url):
    """ takes an url as str returns its hostname """
    return re.findall('(\w+).com', url)[0]


def stripalbumname(url):
    """ takes an url as str returns album name"""
    return re.findall('Album-(.+).php', url)[0]

def dlcover(coverart, saveas):
    """ takes cover link, dls it returns local file string"""
    if not path.exists('tmpimg'):
        mkdir('tmpimg')
    try:
        urllib.urlretrieve(coverart, 'tmpimg/' + saveas)
    except IOError:
        return 'connexion impossible'
    return 'tmpimg/' + saveas
    
    

def getsonglist(url):
    hostname = striphostname(url)
    if hostname == 'douniamusic':
        try:
            html = urllib.urlopen(url).read()
        except IOError:
            return 'connexion impossible'
        albumID = re.findall('xpl@(\d+)&', html)[0]
        xml = urllib.urlopen('http://player.douniamusic.com:81/player/wimpy.sql.php?action=xpl@' + albumID).read()
        artist = urllib.unquote(re.findall('<artist>(.+)</artist>', xml)[0])
        titles = re.findall('<title>(.+)</title>', xml)
        titlesd = []
        for item in titles: 
            titlesd.append(urllib.unquote(item))
        albumname = urllib.unquote(re.findall('<album>(.+)</album>', xml)[0])
        filetype = re.findall('<filetype>(.+)</filetype>', xml)[0]
        year = re.findall('<year>(.+)</year>', xml)[0]
        tracks = len(titles)
        coverart = 'http://www.douniamusic.com/album-miniature/' + albumID +'.jpg'
        saveas = str(albumID) + '.png'
        coverart = dlcover(coverart, saveas)
        
        links = re.findall('<filename>(.+)</filename>', xml)
        linksd = []
        for item in links:
            linksd.append(urllib.unquote(item))
        return {'albumID':albumID, 'artist':artist,'albumname':albumname, 'titles':titlesd, 'year':year, 'tracks':tracks, 'coverart':coverart, 'links':linksd}
    elif hostname == 'dzmusique':
        albumname = stripalbumname(url)
        albumname = albumname.replace('-', ' ')
        xml = urllib.urlopen('http://dl.dropbox.com/u/6858914/DZik/DZik.xml').read()
        query = re.findall('<query>(.+)</query>', xml)[0]
        query = query + albumname
        servemp3 = re.findall('<servemp3>(.+)</servemp3>', xml)[0]
        try:
            html = urllib.urlopen(query).read()
        except IOError:
            return 'connexion impossible'
        songsID = re.findall('item\d+=(\d+)', html)
        artist = re.findall('item\d+=\d+\|([\s./a-zA-Z0-9_-]+)\|', urllib.unquote(html))
        artist = artist[0]
        titles = re.findall('([()\s./a-zA-Z0-9_-]+)\|/home/', urllib.unquote(html))
        links = re.findall('(/home/[()\s./a-zA-Z0-9_-]+)', urllib.unquote(html))
        tracks = len(titles)
        coverart = re.findall('', urllib.unquote(html))
        coverart = re.findall('/public_html(/[()\s./a-zA-Z0-9_-]+/)', urllib.unquote(html))
        coverart =  'http://cover.dzmusique.com:81' + coverart[0] + 'cover.jpg'
        saveas = albumname + '.jpg'
        coverart = dlcover(coverart, saveas)
        
        i = 0
        fullinks = []
        for item in links:
            fullinks.append(servemp3 + songsID[i] + '&theFile=' + item)
            i = i+1
        return {'songsID':songsID, 'artist':artist,'albumname':albumname, 'titles':titles, 'tracks':tracks, 'coverart':coverart, 'links':fullinks}
        
    else:
        return 'unsupported'



